﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AdmBase;
using System.Text;
using Guedes_Library;

public partial class admin_pages_clientes_Novo : TelaBase
{

    private ClienteBusiness clienteBusiness;

    public ClienteBusiness ClienteBusiness
    {
        get
        {
            if (clienteBusiness == null)
                clienteBusiness = new ClienteBusiness();
            return clienteBusiness;
        }
        set { clienteBusiness = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        IsLogado();
        File = fileUpload;
        string id = Request.QueryString["id"];
        if (id != null && !IsPostBack)
        {            
            clients cli = ClienteBusiness.GetById(int.Parse(id));
            if (cli == null)
                Response.Redirect("~/admin/pages/clientes/Default.aspx");
            carregaAlterar(cli);
        }
    }

    public void carregaAlterar(clients cli)
    {       
        txtDescricao.Text = cli.Desc;
        txtUrl.Text = cli.Url;
        hdId.Value = cli.ID.ToString();
        hdFoto.Value = cli.Image;
        img.ImageUrl = ResolveClientUrl("~/File/thumb/"+cli.Image);

    }


    public override void DoReturnPage()
    {
        StringBuilder sb = new StringBuilder();

        sb.Append("top.$get(\"" + img.ClientID + "\").src = '" + ResolveClientUrl("~/File/thumb/"+UrlArquivo) + "';");
        sb.Append("top.$get(\"" + hdFoto.ClientID + "\").value = '" + UrlArquivo + "';");
        // sb.Append("top.$get(\"" + hdThumb.ClientID + "\").value = '" + UrlArquivo + "';");

        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "size", sb.ToString(), true);
    }

    protected void Salvar(object sender, EventArgs e)
    {
        clients cli = new clients();

        cli.ID = 0;
        cli.Url = txtUrl.Text;
        cli.Image = hdFoto.Value;
        cli.Desc = txtDescricao.Text;
        if(hdId.Value != "")
            cli.ID = int.Parse(hdId.Value);

        Retorno retorno = ClienteBusiness.SaveOrUpdate(cli);
        if (!retorno.Error)
            Limpar();
        ShowMensagem("Atenção", retorno.Mensagem);
    }


    public void Limpar()
    {        
        txtDescricao.Text = String.Empty;
        txtUrl.Text = String.Empty;       
        img.ImageUrl = "~/admin/Images/pimenta.png";
    }

}